<?php
class Model_FacultyDocuments extends CCMIS_Model {
    //Database faculty_documents

    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function getAll()
    {
        $sql = "SELECT * FROM faculty_documents";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }

    public function getAllByPublicity($publicityID)
    {
        $sql = "SELECT * FROM faculty_documents WHERE PublicityID LIKE ?";
        $query =  $this->db->query($sql, $publicityID);
        $result = $query->result_array();
        return $result;
    }

    public function getById($id)
    {
        $sql = "SELECT * FROM faculty_documents WHERE DocumentID LIKE ?";
        $query =  $this->db->query($sql, $id);
        $result = $query->row_array();
        return $result;
    }

    public function getByUploaderId($id)
    {
        $sql = "SELECT * FROM faculty_documents WHERE UploaderID LIKE ?";
        $query =  $this->db->query($sql, $id);
        $result = $query->result_array();
        return $result;
    }

   public function add($uploaderID, $documentName, $date, $publicityID, $originalName, $description, $documentType)
   {
       $sql = "INSERT INTO `faculty_documents` (
        `UploaderID`,
        `DocumentName`,
        `DateUploaded`,
        `PublicityID`,
        `OriginalName`,
        `Description`,
        `DocumentType`
        )VALUES (?, ?, ?, ?, ?, ?, ?)";

       $this->db->query($sql, array($uploaderID, $documentName, $date, $publicityID, $originalName, $description, $documentType));
   }

    public function editById($id, $documentName, $publicityId, $description)
    {
        $sql = "UPDATE `faculty_documents`

         SET
        `DocumentName` = ?,
        `PublicityID` = ?,
        `Description` = ?

        WHERE DocumentID = ?
        ";

        $this->db->query($sql, array($documentName, $publicityId, $description, $id));
    }

    public function deleteById($id)
    {
        $sql = "DELETE FROM faculty_documents WHERE DocumentID LIKE ?";
        $query =  $this->db->query($sql, $id);
    }

    public function searchByUploaderIdAndPublicityId($uploaderID, $publicityID)
    {
        $sql = "SELECT * FROM faculty_documents WHERE UploaderID LIKE ? AND PublicityID LIKE ?";
        $query =  $this->db->query($sql, array($uploaderID, $publicityID));
        $result = $query->result_array();
        return $result;
    }

    public function searchByDocumentNameAndPublicityId($documentName, $publicityID)
    {
        if($documentName == '')
        {
            return array();
        }

        $sql = "SELECT * FROM faculty_documents WHERE DocumentName LIKE '%".$documentName."%' AND PublicityID LIKE ?";
        $query =  $this->db->query($sql, $publicityID);
        $result = $query->result_array();
        return $result;
    }

    public function search($documentName, $uploaderIDs, $publicityID)
    {
        $sql = "SELECT * FROM faculty_documents ";

        if($documentName == "" && count($uploaderIDs) <= 1)
            return array();

        if($documentName != '')
        {
            $sql .= "WHERE DocumentName LIKE '%".$documentName."%' ";
        }

        foreach($uploaderIDs as $uploaderID)
        {
            if($uploaderID == '')
                continue;

            if (strpos($sql, "WHERE") !== false)
            {
                $sql .= "OR ";
            }
            else
            {
                $sql .= "WHERE ";
            }

            $sql .= "UploaderID LIKE ".$uploaderID." ";
        }

        $sql .= "AND PublicityID LIKE ".$publicityID." ";
        $query =  $this->db->query($sql);
        $result = $query->result_array();
        return $result;
    }




}
